\subsection{\refactoring{Move Inner To Toplevel}}
This refactoring converts a member type to a toplevel type. Implemented in \sourcelink{TypePromotion/MoveMemberTypeToToplevel.jrag}; see Algorithms~\ref{alg:MoveMemberTypeToToplevel}, \ref{alg:InsertType}, \ref{alg:MakeTypeStatic}.

\begin{algorithm}[p]
\caption{$\refactoring{Move Member Type to Toplevel}(M : \type{MemberType})$}
\label{alg:MoveMemberTypeToToplevel}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java $\cup$ locked names
\medskip
\IF{$M$ is not static}
  \STATE $\refactoringNoExt{Make Type Static}(M)$
\ENDIF
\STATE $p \leftarrow \util{hostPkg}(M)$
\STATE lock all names in $M$
\STATE remove $M$ from its host type
\STATE $\refactoring{Insert Type}(p, M)$
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[p]
\caption{$\refactoring{Insert Type}(p : \type{Package}, T : \type{ClassOrInterface})$}
\label{alg:InsertType}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java $\cup$ locked names
\medskip
\STATE \assert no type or subpackage of same name as $T$ in $p$
\STATE $\util{lockNames}(\util{name}(T))$
\STATE remove modifiers \code{static}, \code{private}, \code{protected} from $T$
\STATE insert $T$ into $p$
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[p]
\caption{$\refactoring{Make Type Static}(M : \type{MemberType})$}
\label{alg:MakeTypeStatic}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java $\cup$ \code{with}, locked names
\medskip
\STATE $[A_n;\ldots;A_1] \leftarrow \text{enclosing types of $M$}$
\FORALL{$i\in\{1,\ldots,n\}$}
  \STATE $f \leftarrow \text{new field of type $A_i$ with name \texttt{this\$i}}$
  \STATE $\refactoring{Insert Field}(M, f)$
  \FORALL{constructors $c$ of $M$}
    \STATE $p \leftarrow \text{parameter of type $A_i$ with name \texttt{this\$i}}$
    \STATE \assert no parameter or variable \texttt{this\$i} in $c$
    \STATE insert $p$ as first parameter of $c$
    \IF{$c$ is chaining}
      \STATE add \texttt{this\$i} as first argument of chaining call
    \ELSE
      \STATE $a \leftarrow \text{new assignment of $p$ to $f$}$
      \STATE insert $a$ after \code{super} call
    \ENDIF
  \ENDFOR
\ENDFOR
\FORALL{constructors $c$ of $M$}
  \FORALL{non-chaining invocations $u$ of $c$}
    \STATE $es \leftarrow \text{enclosing instances of $u$}$
    \STATE \assert $|es|=n$
    \STATE insert $es$ as initial arguments to $u$
    \STATE discard qualifier of $u$, if any
  \ENDFOR
\ENDFOR
\IF{$M$ not in inner class}
  \STATE put modifier \code{static} on $M$
\ENDIF
\FORALL{non-\code{static} callables $m$ of $M$}
  \IF{$m$ has a body}
    \STATE surround body of $m$ by\\
           \code{with(this\$n, ..., this\$1, this) \{...\}}%\\
  \ENDIF
\ENDFOR
\end{algorithmic}
\end{algorithm}
